package com.ztc.ZtcTransportCapitalLog.web;


import com.aqgj.User.bean.User;
import com.ztc.ZtcTransport.bean.ZtcTransport;
import com.ztc.ZtcTransport.service.ZtcTransportManager;
import com.ztc.ZtcTransportCapitalLog.service.ZtcTransportCapitalLogManager;
import com.ztc.util.excel.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("ztc/ztcTransportCapitalLog")
public class ZtcTransportCapitalLogAction {

    @Autowired
    private ZtcTransportCapitalLogManager ztcTransportCapitalLogManager;
    @Autowired
    private ZtcTransportManager ztcTransportManager;

    /**
     * 获取当前公司收支明细.
     * @param session
     * @param pageNumber  页码
     * @param pageSize  每页大小
     * @return
     */
    @ResponseBody
    @RequestMapping("getZtcTransportCapitalLog")
    public HashMap<String,Object> getZtcTransportCapitalLog(HttpSession session, @RequestParam(defaultValue ="1") Integer pageNumber, @RequestParam(defaultValue ="10") Integer pageSize,@RequestParam(defaultValue ="1")Integer type,String startDate,String endDate,String fullName) {
        User user=(User)session.getAttribute("userInfo");
        return ztcTransportCapitalLogManager.searchZtcTransportCapitalLog(user.getTransportId(),pageNumber,pageSize,type, startDate,endDate,fullName);
    }


    /**
     * 导出excel，当前页
     * @param response
     */
    @RequestMapping("exportExcel")
    public void exportFeedBack(HttpServletResponse response, HttpSession session,@RequestParam(defaultValue ="1") Integer pageNumber, @RequestParam(defaultValue ="10") Integer pageSize, String startDate, String endDate,@RequestParam(defaultValue ="1")Integer type,String fullName) {
        User user=(User)session.getAttribute("userInfo");
        String[] file=ifType(type);
        //标题
        String[] title = new String[]{"公司", "交易类型", "交易金额", "余额", "备注", "时间"};
        HashMap<String,Object> map= ztcTransportCapitalLogManager.searchZtcTransportCapitalLog(user.getTransportId(),pageNumber,pageSize,type, startDate,endDate,fullName);
        List<Map<String, Object>> list = (List<Map<String,Object>>) map.get("data");

        String[][] values = new String[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            values[i] = new String[title.length];
            //将对象内容转换成string
            Map<String, Object> obj = list.get(i);
            values[i][0] = obj.get("company").toString();
            values[i][1] = obj.get("type").toString();
            values[i][2] = obj.get("money").toString();
            values[i][3] = obj.get("afterBalance").toString();
            values[i][4] = obj.get("remark").toString();
            values[i][5] = obj.get("createTime").toString();
        }
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(file[1], title, values, null);
        ExcelUtil.setResponseHeader(response,file[0],wb);
    }

    /**
     * 导出excel全部数据
     * @param response
     */
    @RequestMapping("exportAllExcel")
    public void exportAllExcel(HttpServletResponse response, HttpSession session,String startDate, String endDate,@RequestParam(defaultValue ="1")Integer type,String fullName) {
        User user=(User)session.getAttribute("userInfo");
        String[] file=ifType(type);
        //标题
        String[] title = new String[]{"公司", "交易类型", "交易金额", "余额", "备注", "时间"};
        List<Object[]> list= ztcTransportCapitalLogManager.getAllCapitalLog(user.getTransportId(),type,fullName,startDate,endDate);
        String[][] values = new String[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            values[i] = new String[title.length];
            //将对象内容转换成string
            Object[] obj = list.get(i);
            ZtcTransport ztcTransport=ztcTransportManager.get(obj[1].toString());
            values[i][0] = ztcTransport==null?"":ztcTransport.getFullName();
            values[i][1] = ExcelUtil.getCapitalLogType(Integer.valueOf(obj[2].toString()));
            values[i][2] = obj[3].toString();
            values[i][3] = obj[4].toString();
            values[i][4] = obj[6].toString();
            values[i][5] = obj[7].toString();
        }

        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(file[1], title, values, null);
        ExcelUtil.setResponseHeader(response,file[0],wb);
    }

    //文件名
    private String[] ifType(Integer type){
        //文件名
        String fileName ="";
        //sheet名
        String sheetName ="";
        if(type==1){
            fileName="财务资金明细"+System.currentTimeMillis() + ".xls";
            sheetName="财务资金明细";
        }else{
            fileName="公司资金明细"+System.currentTimeMillis() + ".xls";
            sheetName="公司资金明细";
        }
        return new String[]{fileName,sheetName};
    }

    /**
     * 根据公司ID获取最新N条收益信息.
     * @param session
     * @param number 显示条数默认6条
     * @return
     */
    @ResponseBody
    @RequestMapping("getUpCapitalLogList")
    public HashMap<String,Object> getUpCapitalLogList(HttpSession session,@RequestParam(defaultValue ="6") Integer number) {
        User user=(User)session.getAttribute("userInfo");
        return ztcTransportCapitalLogManager.getUpCapitalLogList(user.getTransportId(),number,ztcTransportManager.get(user.getTransportId()).getType());
    }

    /**
     * 根据年份获取该年度每月收益总额.
     * @param session
     * @param year  年份
     * @return
     */
    @ResponseBody
    @RequestMapping("getCapitalTotal")
    public HashMap<String,Object> getCapitalTotal(HttpSession session,Integer year) {
        User user=(User)session.getAttribute("userInfo");
        return ztcTransportCapitalLogManager.getCapitalTotal(user.getTransportId(),year,ztcTransportManager.get(user.getTransportId()).getType());
    }
}